package com.dao.Impl;

import com.dao.RechargerecordDao;
import com.dao.RechargeruleDao;
import com.entity.PageBean;
import com.entity.Rechargerecord;
import com.entity.Rechargerule;
import com.util.JDBC;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class RechargerecordDaoImpl implements RechargerecordDao {
    // 查询每个月的人数的数量，做统计图用
    @Override
    public Map findsum() {
        //select month(rechargerecord.createdTime),sum(rechargeAmount)
        // from rechargerecord group by month(rechargerecord.createdTime) asc
        List<List> lists = JDBC.selectArray("select month(rechargerecord.createdTime),sum(rechargeAmount) " +
                " from rechargerecord group by month(rechargerecord.createdTime)");
        Map map = new HashMap();
        for (List o : lists) {
            map.put(o.get(0), o.get(1));
        }
        return map;
    }

    /**
     * 增加充值记录
     * @param rech
     */
    @Override
    public void addRechargeRecord(Rechargerecord rech) {
        JDBC.sqlUpdate("insert into rechargerecord(id,cardId,rechargeAmount,beforeAmount,afterAmount,ruleId,createdTime,momo) values(null,?,?,?,?,?,now(),?)",
        rech.getCardId(),rech.getRechargeAmount(),rech.getAfterAmount(),rech.getBeforeAmount(),rech.getRuleId(),rech.getMomo()
        );
    }

    /**
     * 查询充值记录
     * @param pageBean
     */
    @Override
    public void findRechargeRecord(PageBean pageBean,int cardId) {
        String sql="select count(*) from rechargerecord where cardId=?";
        List<List> lists = JDBC.selectArray(sql, cardId);
        int a= ((Long)lists.get(0).get(0)).intValue();
        pageBean.setRowCount(a);//设置行数;
        pageBean.setTotalPage();//设置页数
        sql="select user.username,user.cardId,rechargeAmount,afterAmount,beforeAmount,ruleId,r.createdTime,r.momo,re.name from rechargerecord r,user,rechargerule re where r.cardId=user.cardId and r.ruleId=re.id and r.cardId=? limit ?,?";
        List<Map> list = JDBC.selectMap(sql, cardId, (pageBean.getPageNo() - 1) * pageBean.getPageSize(), pageBean.getPageSize());
        pageBean.setData(list);
    }


    /**
     * 开卡时增加充值记录
     * @param rechargerecord
     */
    @Override
    public void insert(Rechargerecord rechargerecord) {
        JDBC.sqlUpdate("insert into rechargerecord(cardId,rechargeAmount,afterAmount,beforeAmount,ruleId,createdTime,staffId,momo) values(?,?,?,0,1,now(),?,?)",
                rechargerecord.getCardId(),
                rechargerecord.getRechargeAmount(),
                rechargerecord.getAfterAmount(),
                rechargerecord.getStaffId(),
                "会员办卡"

        );
    }

}
